﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Web;
using CRMSystemV2.Common;
namespace CRMSystemV2.Web.Sys
{
    public partial class User : PageBase
    {
        private readonly CRMSystemAPI.CRMSystemAPIClient api = new CRMSystemAPI.CRMSystemAPIClient();
        private readonly string[] arrExportColumns = new string[] { "ID", "用户名", "工号", "邮箱", "电话", "是否可用" };
        protected void Page_Load(object sender, EventArgs e)
        {
            //保存搜索关键字的值
            SetValue("username",Request["username"]==null?"":Request["username"]);
            SetValue("jobno", Request["jobno"] == null ? "" : Request["jobno"]);
            SetValue("email", Request["email"] == null ? "" : Request["email"]);
            SetValue("phone", Request["phone"] == null ? "" : Request["phone"]);
            SetValue("isenable", Request["isenable"] == null ? "" : Request["isenable"]);

            if (Request["method"] == null)
            {
                BindData();
            }
            else
            {
                switch (Request["method"].ToLower())
                {
                    case "resetpassword"://重置密码
                        ResetPassword();
                        break;
                    case "delete"://删除
                        Delete();
                        break;                    
                    case "export"://导出
                        Export();
                        break;
                }
            }
        }
        
        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindData()
        {
            //DataTable dt =
            Repeater1.DataSource = api.UserPageView(out rowTotal, out pageTotal, pageSize, pageIndex, keyWords, orderField, orderDirection);
            Repeater1.DataBind(); 
        }
        /// <summary>
        /// 重置密码
        /// </summary>
        private void ResetPassword()
        {
            string id = Request["id"];
            string result = api.ResetPassword(id);
            ResultJson rj = result.DeserializeObject();
            ajaxDone json = new ajaxDone()
            {
                statusCode = rj.status,
                message = rj.message,                
            };
            json.ResponseWrite();
        }
        /// <summary>
        /// 删除,支持批量删除
        /// </summary>
        private void Delete()
        {
            string ids = Request["ids"];
            string result = api.UserDelete(ids);
            ResultJson rj = result.DeserializeObject();
            ajaxDone json = new ajaxDone()
            {
                statusCode = rj.status,
                message = rj.message,
                navTabId = "User"                
            };
            json.ResponseWrite();
        }
        /// <summary>
        /// 导出excel
        /// </summary>
        private void Export()
        {
            DataTable dt = api.UserExportTable(5000,keyWords, orderField, orderDirection);
            string fileName =Server.MapPath("~/file/download/")+string.Format("用户信息{0}.xls",DateTime.Now.ToString("yyyyMMddHHmmss"));
            dt.Export2Excel("用户信息", arrExportColumns, fileName);
            this.SendExcelResponse(fileName);
        }
    }
}